﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dune2013
{
    class Helicoptero : Actor
    {
        private Recolector target = null;
        private Protector protect = null;
        public Helicoptero(Actor target, Protector protect)
        {
            this.rangoRepulsion = 200f;
            this.target = (Recolector)target;
            this.protect = protect;
            position = new Vector2(500,500);
            speed = StaticStuff.rapidezHelicoptero;
            maxTurnSpeed = .1f;
            this.tex = StaticStuff.getTextureById(StaticStuff.tiposTexturas.HELICOPTERO);
        }

        public override void update(int dTime)
        {
            Vector2 totalForce = seek(target);
            tryOrientation(totalForce);
            tryColisions();
            base.update(dTime);
        }

        public void tryColisions() {
            if (tryProtectorColision())
            {
                destroy();
            }
            if (tryTargetColision()) {
                target.restarVida(StaticStuff.danioHelicoptero);
                destroy();
            }
        }

        public void destroy() {
            ActorManager am = ActorManager.getInstance();
            am.borrar(this);
        }


        public bool tryProtectorColision() {
            return tryColisionWith(protect);
        }

        public bool tryTargetColision() {
            return tryColisionWith(target);
        }

        public bool tryColisionWith(Actor act) {
            Vector2 targetSize = act.getSize();
            targetSize = targetSize / 2;
            Vector2 mySize = this.getSize();
            mySize = mySize / 2;
            float distancia = (act.position.X - this.position.X);
            if (distancia < 0) distancia *= -1;
            if (!(distancia < (targetSize.X + mySize.X)))
            {
                return false;
            }
            distancia = (act.position.Y - this.position.Y);
            if (distancia < 0) distancia *= -1;
            if (!(distancia < (targetSize.Y + mySize.Y)))
            {
                return false;
            }
            return true;
        }

    }
}
